<?php

/**
 * Description of HtmlFormElementModel
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
final class HtmlFormElementModel extends HtmlFormElement {

    /**
     * The constructor.
     */
    public function __construct(array $fields = array()) {
        parent::__construct($fields);
    }

    /**
     * ADMIN SPECIFIC METHODS.
     */

    /**
     * Getting data for all elements within a given HTML form.
     *
     * @param int $htmlFormId
     * @param int $pageNumber
     * @return array
     */
    public static function getHtmlFormElementData($htmlFormId, $searchString = NULL, $pageNumber = 1) {

        /**
         * Get paginated results.
         */
        $queryCount = new Query('
            SELECT COUNT(*) AS `count`
            FROM `html_form_element`
            :where
            :and :search;
        ');
        $queryCount = AdminUtils::queryProcessing($queryCount, 'html_form_element', 'html_form', $htmlFormId, $searchString);
        $query = new Query('
            SELECT :select
            FROM `html_form_element`
            :join
            :where
            :and :search
            ORDER BY `html_form_element`.`html_form`, `html_form_element`.`id`
            LIMIT :offset, :row_count;
        ');
        $query = AdminUtils::queryProcessing($query, 'html_form_element', 'html_form', $htmlFormId, $searchString);

        $pagination = new Pagination(
            $query,
            $queryCount,
            $pageNumber
        );
        $output = array();
        $output['tabulated'] = $pagination->getRows();
        $output['navigation_array'] = $pagination->getNavigationArray();

        /**
         * Get form data.
         */
        if ($htmlFormId != NULL) {
            $htmlForm = new HtmlForm;
            $htmlForm->load($htmlFormId);
            $output['html_form'] = $htmlForm->getViewFields();
        }

        return $output;
    }

}